package cn.hesion.nio;
/**
 * ClassName: NIOFileChannel <br/>
 * Description: <br/>
 * date: 2021/1/21 14:20<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

/**
 * @program: NettyLearn
 * @description: 文件通道示例
 * @author: hesion
 * @create: 2021-01-21 14:20
 **/
public class NIOFileChannel {
    public static void main(String[] args) {
        String context = "Hello Chennal";
        try {
            //文件输出流-channel
            FileOutputStream fileOutputStream = new FileOutputStream("d://file.txt");

            //从输入流中获取Channel
            FileChannel channel = fileOutputStream.getChannel();
            //  创建一个ByteBuffer
            // 将数据放入ByteBuffer中
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put(context.getBytes());

            byteBuffer.flip();

            channel.write(byteBuffer);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
